﻿@using System.ComponentModel
@using AntDesign.TableModels

<Table @ref="table"
       TItem="WeatherForecast"
       DataSource="@forecasts"
       Total="_total"
       @bind-PageIndex="_pageIndex"
       @bind-PageSize="_pageSize"
       @bind-SelectedRows="selectedRows"
       OnChange="OnChange"
       Size="TableSize.Small"
       RowKey="x=>x.Id"
      >
  <Selection Key="@(context.Id.ToString())" />
  <PropertyColumn Property="c=>c.Id" Sortable />
  <PropertyColumn Property="c=>c.Date" Format="yyyy-MM-dd" Sortable />
  <PropertyColumn Property="c=>c.TemperatureC" Sortable />
  <PropertyColumn Title="Temp. (F)" Property="c=>c.TemperatureF" />
  <PropertyColumn Title="Hot" Property="c=>c.Hot">
    <Switch @bind-Value="@context.Hot"></Switch>
  </PropertyColumn>
  <PropertyColumn Property="c=>c.Summary" Sortable />
  <ActionColumn Title="Action">
    <Space>
      <SpaceItem><Button Danger OnClick="()=>Delete(context.Id)">Delete</Button></SpaceItem>
    </Space>
  </ActionColumn>
</Table>

<br />
<p>PageIndex: @_pageIndex | PageSize: @_pageSize | Total: @_total</p>

<br />
<h5>selections:</h5>
@if (selectedRows != null && selectedRows.Any())
{
  <Button Danger Size="small" OnClick="@(e => { selectedRows = null; })">Clear</Button>

  @foreach (var selected in selectedRows)
  {
    <Tag @key="selected.Id" Closable OnClose="e=>RemoveSelection(selected.Id)">@selected.Id - @selected.Summary</Tag>
  }
}

<Button OnClick="()=> { _pageIndex--; }">Previous page</Button>
<Button OnClick="()=> { _pageIndex++; }">Next Page</Button>

@using System.Text.Json;
@code {

  WeatherForecast[] forecasts;

  IEnumerable<WeatherForecast> selectedRows;
  ITable table;

  int _pageIndex = 1;
  int _pageSize = 10;
  int _total = 0;

  protected override async Task OnInitializedAsync()
  {
    forecasts = await GetForecastAsync(1, 50);
    _total = 50;
  }

  public class WeatherForecast
  {
    public int Id { get; set; }

    [DisplayName("Date")]
    public DateTime? Date { get; set; }

    [DisplayName("Temp. (C)")]
    public int TemperatureC { get; set; }

    [DisplayName("Summary")]
    public string Summary { get; set; }

    public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

    public bool Hot { get; set; }
  }

  private static readonly string[] Summaries = new[]
  {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
  };

  public async Task OnChange(QueryModel<WeatherForecast> queryModel)
  {
    Console.WriteLine(JsonSerializer.Serialize(queryModel));
  }

  public Task<WeatherForecast[]> GetForecastAsync(int pageIndex, int pageSize)
  {
    var rng = new Random();
    return Task.FromResult(Enumerable.Range((pageIndex - 1) * pageSize + 1, pageSize).Select(index =>
    {
      var temperatureC = rng.Next(-20, 55);
      return new WeatherForecast
        {
          Id = index,
          Date = DateTime.Now.AddDays(index),
          TemperatureC = temperatureC,
          Summary = Summaries[rng.Next(Summaries.Length)],
          Hot = temperatureC > 30,
        };
    }).ToArray());
  }

  public void RemoveSelection(int id)
  {
    var selected = selectedRows.Where(x => x.Id != id);
    selectedRows = selected;
  }

  private void Delete(int id)
  {
    forecasts = forecasts.Where(x => x.Id != id).ToArray();
    _total = forecasts.Length;
  }
}
